//
//  InstantFeedbackController.m
//  ActiveDeck
//
//  Created by Michael Cohen on 9/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InstantFeedbackController.h"

@implementation InstantFeedbackController

@synthesize delegate, feedbackLabel, correctLabel, numIncorrectLabel, numCorrectLabel, nextButton, okButton, page, submittedAnswer, submittedWordCategories, hasNextPage, numberCorrect, numberIncorrect;

@protocol feedbackVCDelegate
-(void)buttonClicked:(UIButton*)nextButton inView:(UIView*)self;
-(void)buttonClicked:(UIButton*)okButton inView:(UIView*)self;
@end

/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
 */

- (void)dealloc
{
    [nextButton release];
    [okButton release];
    [page release];
    [submittedAnswer release];
    [submittedWordCategories release];
    [correctLabel release];
    [feedbackLabel release];
    [numCorrectLabel release];
    [numIncorrectLabel release];
    [super dealloc];
}

-(void) closeFeedback:(id)sender{
    [self.view removeFromSuperview];
    [[self delegate] windowClosed];
}

-(void) goToNextPage:(id)sender{
    [self.view removeFromSuperview];
    [[self delegate] showNextPage];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(hasNextPage){
        [nextButton setHidden:NO];
        [okButton setHidden:YES];
    } else{
        [nextButton setHidden:YES];
        [okButton setHidden:NO];
    }    
    
    [self loadInstantFeedback];
    // Do any additional setup after loading the view from its nib.
}

- (void)loadInstantFeedback
{
    AD_Grade *grade = page.grade;
    int correctCount = 0;
    int incorrectCount = 0;
    if([page.pageType isEqualToString:@"MultipleChoiceQuestion"] || [page.pageType isEqualToString:@"ShortAnswerQuestion"]){
        for(NSString *correctAnswer in grade.CorrectAnswers){
            if([submittedAnswer isEqualToString:correctAnswer]){
                correctCount++;
            } else {
                incorrectCount++;
            }
        } 
        
        if(correctCount == [grade.CorrectAnswers count]){
            [correctLabel setText:[grade.correctAnswerFeedback stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            page.grade.hasAnsweredCorrectly = YES;
        } else {
            [correctLabel setText:[grade.incorrectAnswerFeedback stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            page.grade.hasAnsweredCorrectly = NO;
        }    
    } else if ([page.pageType isEqualToString:@"Categories"]){        
        
        for(id word in grade.CorrectWordCategories){
            NSString *category = [grade.CorrectWordCategories objectForKey:word]; 
            
            NSString *submittedCategory = [submittedWordCategories objectForKey:word];
            
            if([category isEqualToString:submittedCategory]){
                correctCount++;                
            } else {
                incorrectCount++;
            }
        }
        
        if(correctCount == [grade.CorrectWordCategories count]){
            [correctLabel setText:[grade.correctAnswerFeedback stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            page.grade.hasAnsweredCorrectly = YES;
        } else {
            [correctLabel setText:[grade.incorrectAnswerFeedback stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            page.grade.hasAnsweredCorrectly = NO;
        }    
    }
    
    
    
    numberCorrect += correctCount;
    numberIncorrect += incorrectCount;
    
    [numCorrectLabel setText:[NSString stringWithFormat:@"%d", numberCorrect]];
    [numIncorrectLabel setText:[NSString stringWithFormat:@"%d", numberIncorrect ]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
   // [self.view removeFromSuperview];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

@end
